// multiplicity distributions of dcs after the dipole magnet
// Jul. 9, 2024

void a(vector<TH1 *> &v, TObject *h){ v.push_back((TH1 *)h); }
void muldc(){
  TFile *f = new TFile(getenv("TMP1"), "UPDATE");
  TTree *etf = (TTree *)f->Get("etf");
  vector<TH1 *> v;
  TH1 *h0;
  char zcut[64] = "";
  static const int Z = 20;
  if(Z > 0) sprintf(zcut, "az0>%.2f && az0<%.2f", Z-0.5, Z+0.5);

  const char xuv[3] = {'X', 'U', 'V'};
  char hname[64]{}, ly[256]{}, cut[512]{};
  for(int i = 0; i < 3; i++){
    for(int j = 0; j < 3; j++){
      for(int k = 0; k < 2; k++){
        sprintf(ly, "dc%d%c%d", i, xuv[j], k);
        sprintf(hname, "h%s", ly);
        h0 = new TH1F(hname, Form("Multiplicity of %s", ly), 13, -1.5, 11.5);
        h0->GetXaxis()->SetTitle("Multiplicity");
        string cut = Form("%s", zcut);
        etf->Project(hname, Form("Length$(%s)", ly), cut.data());
        a(v, h0);
        cout << "Processing " << ly << ": " << zcut << "...\r" << flush;
      } // end for over x1-2
    } // end for over x,u,v
  } // end for over dcs
  cout << endl;

  TCanvas *c1 = new TCanvas("c", "1-D X Multiplicity Distribution for post Mag dcs", 1680, 1050);
  c1->Divide(6, 3);
  for(int i = 0; i < 18; i++){
    c1->cd(i+1); v[i]->Draw();
  } // end for
  // c1->Print("muldc.pdf");
} // end muldc
